<template>
  <view class="container">
    <!-- 积分余额 -->
    <view class="balance-card">
      <view class="balance-info">
        <text class="balance-label">当前积分余额</text>
        <text class="balance-value">{{ formatPoint(accountInfo.pointBalance) }}</text>
      </view>
      <view class="balance-detail">
        <text class="detail-text">可提现余额：{{ formatPrice(accountInfo.balance) }}</text>
        <text class="detail-text">总余额：{{ formatPrice(accountInfo.totalBalance) }}</text>
      </view>
    </view>

    <!-- 平台筛选 -->
    <view class="filter-bar">
      <view 
        class="filter-item" 
        :class="{ 'active': currentPlatform === '' }"
        @click="changePlatform('')"
      >
        <text>全部</text>
      </view>
      <view 
        class="filter-item" 
        :class="{ 'active': currentPlatform === '1' }"
        @click="changePlatform('1')"
      >
        <text>广告奖励</text>
      </view>
      <view 
        class="filter-item" 
        :class="{ 'active': currentPlatform === '2' }"
        @click="changePlatform('2')"
      >
        <text>异业分佣</text>
      </view>
    </view>

    <!-- 积分明细列表 -->
    <view class="history-list">
      <view v-if="loading" class="loading">
        <text>加载中...</text>
      </view>
      
      <view v-else-if="pointList.length === 0" class="empty">
        <text class="empty-icon">💰</text>
        <text class="empty-text">暂无积分记录</text>
      </view>
      
      <view v-else class="points">
        <view 
          class="point-item" 
          v-for="item in pointList" 
          :key="item.id"
        >
          <!-- 积分信息 -->
          <view class="point-info">
            <text class="point-type">{{ getPlatformName(item.platform) }}</text>
            <text class="point-time">{{ formatTime(item.createTime) }}</text>
          </view>
          
          <!-- 积分变动 -->
          <view class="point-change">
            <text 
              class="point-amount" 
              :class="{ 'increase': item.increase === '1', 'decrease': item.increase === '2' }"
            >
              {{ item.increase === '1' ? '+' : '-' }}{{ item.point }}
            </text>
            <text class="point-unit">分</text>
          </view>
          
          <!-- 变动类型 -->
          <view class="change-type">
            <text class="type-text">{{ item.increase === '1' ? '收入' : '支出' }}</text>
          </view>
        </view>
      </view>
      
      <!-- 加载更多 -->
      <view v-if="hasMore && !loading" class="load-more">
        <button class="load-more-btn" @click="loadMore">加载更多</button>
      </view>
    </view>
  </view>
</template>

<script>
import { getPointHistory } from '@/api/point.js'
import { getCurrentAccount } from '@/api/user.js'

export default {
  data() {
    return {
      accountInfo: {}, // 账户信息
      pointList: [], // 积分明细列表
      currentPlatform: '', // 当前筛选平台
      loading: false, // 加载状态
      hasMore: true, // 是否还有更多
      pageIndex: 1, // 当前页码
      pageSize: 10, // 每页条数
      total: 0 // 总数量
    }
  },
  onLoad() {
    this.init()
  },
  onShow() {
    // 每次显示页面时刷新账户信息
    this.loadAccountInfo()
  },
  onPullDownRefresh() {
    // 下拉刷新
    this.refresh()
  },
  onReachBottom() {
    // 上拉加载更多
    if (this.hasMore && !this.loading) {
      this.loadMore()
    }
  },
  methods: {
    // 初始化
    async init() {
      await this.loadAccountInfo()
      this.loadPointList()
    },
    
    // 加载账户信息
    async loadAccountInfo() {
      try {
        const res = await getCurrentAccount()
        this.accountInfo = res.data
      } catch (error) {
        console.error('加载账户信息失败:', error)
      }
    },
    
    // 加载积分明细列表
    async loadPointList(isRefresh = false) {
      if (this.loading) return
      
      this.loading = true
      
      try {
        const params = {
          pageIndex: isRefresh ? 1 : this.pageIndex,
          pageSize: this.pageSize
        }
        
        // 如果有平台筛选，添加到参数中
        if (this.currentPlatform !== '') {
          params.platform = this.currentPlatform
        }
        
        const res = await getPointHistory(params)
        
        if (isRefresh) {
          this.pointList = res.data.list || []
          this.pageIndex = 1
        } else {
          this.pointList = [...this.pointList, ...(res.data.list || [])]
        }
        
        this.total = res.data.total || 0
        this.hasMore = this.pointList.length < this.total
        
        if (!isRefresh) {
          this.pageIndex++
        }
        
      } catch (error) {
        console.error('加载积分明细失败:', error)
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      } finally {
        this.loading = false
        uni.stopPullDownRefresh()
      }
    },
    
    // 刷新
    refresh() {
      this.pageIndex = 1
      this.hasMore = true
      this.loadPointList(true)
    },
    
    // 加载更多
    loadMore() {
      this.loadPointList()
    },
    
    // 切换平台筛选
    changePlatform(platform) {
      this.currentPlatform = platform
      this.refresh()
    },
    
    // 获取平台名称
    getPlatformName(platform) {
      const platformMap = {
        '1': '穿山甲广告',
        '2': 'Taku异业'
      }
      return platformMap[platform] || '未知平台'
    },
    
    // 格式化积分
    formatPoint(point) {
      return (point || 0).toFixed(2) + '分'
    },
    
    // 格式化价格
    formatPrice(price) {
      return (price / 100).toFixed(2) + '元'
    },
    
    // 格式化时间
    formatTime(time) {
      if (!time) return ''
      return time.replace(' ', '\n')
    }
  }
}
</script>

<style scoped>
.container {
  min-height: 100vh;
  background: #f5f5f5;
}

.balance-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  margin: 30rpx;
  border-radius: 12rpx;
  padding: 40rpx;
  color: #fff;
}

.balance-info {
  text-align: center;
  margin-bottom: 30rpx;
}

.balance-label {
  font-size: 28rpx;
  opacity: 0.8;
  display: block;
  margin-bottom: 10rpx;
}

.balance-value {
  font-size: 48rpx;
  font-weight: bold;
  display: block;
}

.balance-detail {
  display: flex;
  justify-content: space-between;
}

.detail-text {
  font-size: 24rpx;
  opacity: 0.8;
}

.filter-bar {
  display: flex;
  background: #fff;
  margin: 0 30rpx 30rpx;
  border-radius: 12rpx;
  padding: 10rpx;
}

.filter-item {
  flex: 1;
  text-align: center;
  padding: 20rpx 0;
  font-size: 28rpx;
  color: #666;
  border-radius: 8rpx;
  margin: 0 5rpx;
  transition: all 0.3s;
}

.filter-item.active {
  background: #667eea;
  color: #fff;
}

.history-list {
  padding: 0 30rpx;
}

.loading, .empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400rpx;
  color: #999;
}

.empty-icon {
  font-size: 80rpx;
  margin-bottom: 20rpx;
}

.empty-text {
  font-size: 28rpx;
}

.point-item {
  background: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.point-info {
  flex: 1;
  margin-right: 20rpx;
}

.point-type {
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 10rpx;
}

.point-time {
  font-size: 24rpx;
  color: #999;
  line-height: 1.4;
}

.point-change {
  display: flex;
  align-items: center;
  margin-right: 20rpx;
}

.point-amount {
  font-size: 36rpx;
  font-weight: bold;
  margin-right: 8rpx;
}

.point-amount.increase {
  color: #52c41a;
}

.point-amount.decrease {
  color: #ff4d4f;
}

.point-unit {
  font-size: 24rpx;
  color: #999;
}

.change-type {
  text-align: center;
}

.type-text {
  font-size: 22rpx;
  color: #999;
}

.load-more {
  text-align: center;
  padding: 30rpx 0;
}

.load-more-btn {
  padding: 20rpx 60rpx;
  background: #fff;
  color: #667eea;
  font-size: 28rpx;
  border-radius: 8rpx;
  border: 2rpx solid #667eea;
}

.load-more-btn::after {
  border: none;
}
</style>
